local skel = fk.CreateSkill {
  name = "emo__guanshi",
}

Fk:loadTranslationTable{
  ["emo__guanshi"] = "观时",
  [":emo__guanshi"] = "若上一张牌为其他角色使用，你可以将一张牌当之使用，且若下一张牌为其他角色使用，结束当前阶段。",

  ["#emo__guanshi"] = "观时：你可以将一张牌当【%arg】使用",
  ["@@emo__guanshi"] = "观时",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  prompt = function (self, player, selected_cards, selected)
    local mark = player:getMark(skel.name)
    if type(mark) ~= "string" then
      mark = "?"
    end
    return "#emo__guanshi:::" .. mark
  end,
  pattern = ".",
  handly_pile = true,
  filter_pattern = {
    min_num = 1, max_num = 1, pattern = "."
  },
  card_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 or player:getMark(skel.name) == 0 then return nil end
    local c = Fk:cloneCard(player:getMark(skel.name))
    c.skillName = skel.name
    c:addSubcard(cards[1])
    return c
  end,
  enabled_at_play = function(self, player)
    local mark = player:getMark(skel.name)
    return mark ~= 0
  end,
  enabled_at_response = function (self, player, response)
    if not response and Fk.currentResponsePattern then
      local mark = player:getMark(skel.name)
      return mark ~= 0 and Exppattern:Parse(Fk.currentResponsePattern):matchExp(mark)
    end
  end,
})

skel:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  anim_type = "negative",
  can_trigger = function (self, event, target, player, data)
    return player:getMark("@@emo__guanshi") ~= 0 and target ~= player
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@@emo__guanshi", 0)
    local phase = player.room.logic:getCurrentEvent():findParent(GameEvent.Phase)
    if phase then
      phase.data.phase_end = true
    end
  end,
})

skel:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    if target ~= player then
      player.room:setPlayerMark(player, skel.name, data.card.name)
    else
      player.room:setPlayerMark(player, skel.name, 0)
      player.room:setPlayerMark(player, "@@emo__guanshi", table.contains(data.card.skillNames, skel.name) and 1 or 0)
    end
  end,
})

return skel
